﻿namespace Vdc.Interface
{
    using Vdc.Interface.Interfaces;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    public class VdcTextBox : TextBox, IPostBackDataHandler, IPostBackEventHandler, IVdcControl
    {
        private static Hashtable availableClientIds = new Hashtable();
        private string clientObjectID;
        private VdcTextBoxClientSideEvents clientSideEvents = new VdcTextBoxClientSideEvents();
        private VdcTextBoxCssSettings cssSettings = new VdcTextBoxCssSettings();
        private List<HtmlLink> htmlLinksAddedToHead;
        private bool IsFocused;
        private VdcDiv mainContainer;
        internal ScriptManagerHelper MyScriptManager;
        private Literal scriptContainerContent;
        private TextBox textBox;

        protected override void CreateChildControls()
        {
            this.mainContainer = new VdcDiv();
            this.mainContainer.ID = this.GetContainerID();
            this.mainContainer.ToolTip = this.ToolTip;
            this.textBox = VdcCloner.CloneTextBox(this);
            this.textBox.ID = this.GetTextBoxID();
            this.textBox.Text = "";
            if (this.TextMode != TextBoxMode.MultiLine)
            {
                this.mainContainer.Width = !string.IsNullOrEmpty(this.Width.ToString()) ? this.Width : Unit.Pixel(150);
                this.mainContainer.CssClass = this.Enabled ? this.CssSettings.Container_Normal : this.CssSettings.Container_Disabled;
                this.textBox.CssClass = this.CssSettings.InputElement;
                VdcDiv div = this.CreateSection("ob_iTL");
                VdcDiv div2 = this.CreateSection("ob_iTR");
                VdcDiv div3 = this.CreateSection("ob_iTC");
                div3.Controls.Add(this.textBox);
                if (VdcControlFactory.IsDesignTimeRender(this) && string.IsNullOrEmpty(this.FolderStyle))
                {
                    div.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textbox.png");
                    div2.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textbox.png");
                    div3.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textbox.png");
                }
            }
            else
            {
                this.mainContainer.CssClass = this.Enabled ? this.CssSettings.Container_MultiLine_Normal : this.CssSettings.Container_MultiLine_Disabled;
                this.mainContainer.Height = this.Height;
                VdcDiv div4 = this.CreateSection("ob_iTaTL");
                VdcDiv div5 = this.CreateSection("ob_iTaTC");
                VdcDiv div6 = this.CreateSection("ob_iTaTR");
                VdcDiv div7 = this.CreateSection("ob_iTaML");
                VdcDiv div8 = this.CreateSection("ob_iTaMC");
                VdcDiv div9 = this.CreateSection("ob_iTaMR");
                VdcDiv div10 = this.CreateSection("ob_iTaBL");
                VdcDiv div11 = this.CreateSection("ob_iTaBC");
                VdcDiv div12 = this.CreateSection("ob_iTaBR");
                if (VdcControlFactory.IsDesignTimeRender(this) && string.IsNullOrEmpty(this.FolderStyle))
                {
                    div4.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textarea-vertical.png");
                    div5.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textarea-vertical.png");
                    div6.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textarea-vertical.png");
                    div7.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textarea-horizontal.png");
                    div9.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textarea-horizontal.png");
                    div10.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textarea-vertical.png");
                    div11.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textarea-vertical.png");
                    div12.Style["background-image"] = VdcControlFactory.GetEmbeddedImageUrl(this, "textarea-vertical.png");
                }
                if (!string.IsNullOrEmpty(this.Width.ToString()))
                {
                    this.mainContainer.Width = this.Width;
                }
                else if (this.Columns == 0)
                {
                    this.mainContainer.Width = Unit.Pixel(150);
                }
                else
                {
                    this.mainContainer.Style["width"] = this.Columns.ToString() + "em";
                }
                if (!string.IsNullOrEmpty(this.Height.ToString()))
                {
                    this.mainContainer.Height = this.Height;
                }
                else if (this.Rows == 0)
                {
                    this.mainContainer.Height = Unit.Pixel(50);
                }
                else
                {
                    this.mainContainer.Style["height"] = (((this.Rows + 1) * 1.3)).ToString() + "em";
                }
                div8.Controls.Add(this.textBox);
            }
            VdcControlFactory.CreateChildControls(this);
            if (VdcControlFactory.IsDesignTimeRender(this))
            {
                this.mainContainer.Style["float"] = "left";
                this.textBox.Style["display"] = "none";
            }
            base.CreateChildControls();
        }

        internal virtual VdcDiv CreateSection(string cssClass)
        {
            VdcDiv child = new VdcDiv
            {
                CssClass = cssClass
            };
            this.mainContainer.Controls.Add(child);
            return child;
        }

        public override void Focus()
        {
            this.IsFocused = true;
            base.Focus();
        }

        protected virtual string GetContainerID()
        {
            return ("ob_iT" + this.ClientObjectID + "Container");
        }

        public static string GetLinkToEmbeddedStyleSheet()
        {
            return "Vdc.Interface.VdcTextBox.resources.styles.black_glass.style.css";
        }

        protected virtual string GetTextBoxID()
        {
            return this.UniqueID;
        }

        protected override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            return base.LoadPostData(postDataKey, postCollection);
        }

        string IVdcControl.CreateClientScript(bool createOnlyClientObject)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" if(typeof(" + this.ClientObjectID + ")!='undefined'){if(typeof(" + this.ClientObjectID + ".destruct)=='function'){" + this.ClientObjectID + ".destruct();}}");
            if (!createOnlyClientObject)
            {
                builder.Append("var " + this.ClientObjectID + ";");
                builder.Append("function initTextBox__" + this.ClientObjectID + "(attempts){ ");
                builder.Append("if(typeof(vdcTextBoxJSLoaded) == \"undefined\" || typeof(Vdc.Interface.VdcTextBox) == \"undefined\") {if(attempts<30){window.setTimeout(\"initTextBox__" + this.ClientObjectID + "(\" + (attempts+1) + \")\", 500);}else{alert(\"There was a problem referencing TextBox javascript files.  Please contact vdccontrol@gmail.com for support.\");}return;}");
                builder.Append("try{");
            }
            builder.Append(this.ClientObjectID + " = new Vdc.Interface.VdcTextBox(");
            builder.Append("\"" + this.ClientObjectID + "\",");
            builder.Append("\"" + this.mainContainer.ClientID + "\",");
            builder.Append("\"" + this.textBox.ClientID + "\",");
            builder.Append("\"" + this.WatermarkText.Replace("\"", "\\\"") + "\",");
            builder.Append(this.Enabled.ToString().ToLower() + ",");
            builder.Append(this.IsFocused.ToString().ToLower() + ",");
            bool flag2 = this.TextMode == TextBoxMode.MultiLine;
            builder.Append(flag2.ToString().ToLower() + ",");
            builder.Append(this.ClientSideEvents.SerializeToJSON() + ",");
            builder.Append(this.AutoPostBack.ToString().ToLower() + ",");
            if (!this.AutoPostBack)
            {
                builder.Append("null,");
            }
            else
            {
                builder.Append("function() {" + this.Page.ClientScript.GetPostBackEventReference(this, this.UniqueID) + "},");
            }
            builder.Append(this.CssSettings.SerializeToJSON(this.TextMode == TextBoxMode.MultiLine) + ",");
            builder.Append("\"" + this.ControlsToEnable + "\",");
            builder.Append(createOnlyClientObject.ToString().ToLower());
            builder.Append(");");
            if (!createOnlyClientObject)
            {
                builder.Append("}catch(e){alert(\"There was an error initializing TextBox with ID " + this.ClientObjectID + ".\\n\\n\" + e.message + \"\\n\\nPlease contact vdccontrol@gmail.com for help.\");}}");
                builder.Append(" initTextBox__" + this.ClientObjectID + "(0); ");
            }
            return builder.ToString();
        }

        void IVdcControl.ForceCreateChildControls()
        {
            this.CreateChildControls();
        }

        protected override void OnInit(EventArgs e)
        {
            this.MyScriptManager = new ScriptManagerHelper(this);
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            this.textBox.Text = !string.IsNullOrEmpty(this.Text) ? this.Text : this.WatermarkText;
            VdcControlFactory.RegisterClientScripts(this, null, null);
            base.OnPreRender(e);
        }

        public void RaisePostBackEvent(string eventArgument)
        {
        }

        protected override void RaisePostDataChangedEvent()
        {
            base.RaisePostDataChangedEvent();
        }

        public static void RegisterClientScripts(Control control)
        {
            ScriptManagerHelper helper = new ScriptManagerHelper(control);
            if (!helper.IsClientScriptIncludeRegistered("VdcCore"))
            {
                helper.RegisterClientScriptInclude("VdcCore", control.Page.ClientScript.GetWebResourceUrl(typeof(VdcTextBox), "Vdc.Interface.VdcCore.resources.scripts.VdcCore.min.js"));
            }
            if (!helper.IsClientScriptIncludeRegistered("VdcControlFactory"))
            {
                helper.RegisterClientScriptInclude("VdcControlFactory", control.Page.ClientScript.GetWebResourceUrl(typeof(VdcTextBox), "Vdc.Interface.VdcCore.resources.scripts.VdcControlFactory.min.js"));
            }
            if (!helper.IsClientScriptIncludeRegistered("VdcTextBox"))
            {
                helper.RegisterClientScriptInclude("VdcTextBox", control.Page.ClientScript.GetWebResourceUrl(typeof(VdcTextBox), "Vdc.Interface.VdcTextBox.resources.scripts.VdcTextBox.min.js"));
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.EnsureChildControls();
            VdcControlFactory.Render(this, writer);
        }

        protected string ClientObjectID
        {
            get
            {
                if (string.IsNullOrEmpty(this.clientObjectID))
                {
                    this.clientObjectID = VdcControlFactory.GetClientObjectID(this);
                }
                return this.clientObjectID;
            }
            set
            {
                this.clientObjectID = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), PersistenceMode(PersistenceMode.InnerProperty), Bindable(true), Category("Client-Side Events"), Description("Property used to define the Client-Side Events."), NotifyParentProperty(true)]
        public VdcTextBoxClientSideEvents ClientSideEvents
        {
            get
            {
                return this.clientSideEvents;
            }
            set
            {
                this.clientSideEvents = value;
            }
        }

        [Description("Gets or sets a value indicating the list of controls that will be enabled when the text is changed."), Bindable(true)]
        public string ControlsToEnable
        {
            get
            {
                object obj2 = this.ViewState["ControlsToEnable"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["ControlsToEnable"] = value;
            }
        }

        [Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), PersistenceMode(PersistenceMode.InnerProperty), Category("CSS Settings"), Description("Property used to define the CSS Settings."), NotifyParentProperty(true)]
        public VdcTextBoxCssSettings CssSettings
        {
            get
            {
                return this.cssSettings;
            }
            set
            {
                this.cssSettings = value;
            }
        }

        [Description("The folder containing the css stylesheet."), Bindable(true)]
        public string FolderStyle
        {
            get
            {
                object obj2 = this.ViewState["FolderStyle"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["FolderStyle"] = value;
            }
        }

        Hashtable IVdcControl.AvailableClientIds
        {
            get
            {
                return availableClientIds;
            }
        }

        VdcDiv IVdcControl.Container
        {
            get
            {
                return this.mainContainer;
            }
        }

        bool IVdcControl.HasChildControlsCreated
        {
            get
            {
                return base.ChildControlsCreated;
            }
        }

        List<HtmlLink> IVdcControl.HtmlLinksAddedToHead
        {
            get
            {
                if (this.htmlLinksAddedToHead == null)
                {
                    this.htmlLinksAddedToHead = new List<HtmlLink>();
                }
                return this.htmlLinksAddedToHead;
            }
        }

        bool IVdcControl.IsDesignTimeRender
        {
            get
            {
                return base.DesignMode;
            }
        }

        string IVdcControl.LinkToEmbeddedStyleSheet
        {
            get
            {
                if (this.TextMode != TextBoxMode.MultiLine)
                {
                    return GetLinkToEmbeddedStyleSheet();
                }
                return GetLinkToEmbeddedStyleSheet().Replace("style.css", "style_multiline.css");
            }
        }

        Literal IVdcControl.ScriptContainerContent
        {
            get
            {
                return this.scriptContainerContent;
            }
            set
            {
                this.scriptContainerContent = value;
            }
        }

        ScriptManagerHelper IVdcControl.ScriptManager
        {
            get
            {
                return this.MyScriptManager;
            }
            set
            {
                this.MyScriptManager = value;
            }
        }

        bool IVdcControl.StyleSheetReferenceAdded
        {
            get
            {
                string key = (this.TextMode != TextBoxMode.MultiLine) ? "vdcTextBox_Single_StyleSheetAdded" : "vdcTextBox_Multiple_StyleSheetAdded";
                return HttpContext.Current.Items.Contains(key);
            }
            set
            {
                string key = (this.TextMode != TextBoxMode.MultiLine) ? "vdcTextBox_Single_StyleSheetAdded" : "vdcTextBox_Multiple_StyleSheetAdded";
                if (value)
                {
                    HttpContext.Current.Items.Add(key, true);
                }
                else
                {
                    HttpContext.Current.Items.Remove(key);
                }
            }
        }

        [Bindable(true), Description("The watermark text of the text box.")]
        public string WatermarkText
        {
            get
            {
                object obj2 = this.ViewState["WatermarkText"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["WatermarkText"] = value;
            }
        }
    }
}

